package org.zfes.snowier.cloud.zbss.metting.router.service.tenant;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.web.controller.BaseController;
import org.zfes.snowier.auth.shiro.util.SecurityUtil;
import org.zfes.snowier.cloud.zbss.metting.biz.dto.MettingsAttachesCreateDto;
import org.zfes.snowier.cloud.zbss.metting.biz.dto.MettingsCreateDto;
import org.zfes.snowier.cloud.zbss.metting.biz.dto.MettingsUpdateDto;
import org.zfes.snowier.cloud.zbss.metting.biz.model.MettingsAttaches;
import org.zfes.snowier.cloud.zbss.metting.biz.service.IMettingsAttachesService;
import org.zfes.snowier.cloud.zbss.metting.biz.service.IMettingsService;
import org.zfes.snowier.common.file.FileResInfo;
import org.zfes.snowier.common.file.disk.UploadUitl;
@Controller("org.zfes.snowier.cloud.zbss.metting.router.service.tenant.MettingsController") 
@RequestMapping("/met/service/tenant/mettings")
public class MettingsController extends BaseController {

	
	@Autowired
	private IMettingsService mettingsService;
	@Autowired
	private IMettingsAttachesService mettingsAttachesService;
	
	
    @RequestMapping(value="/loadMettingsDataSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object loadMettingsDataSet(@RequestParam Map<String,Object> params){
    	params.put("tenantId", SecurityUtil.getTenantId().orElseThrow(()->ZAlert.newSLE("当前用户未分配租户")));
	    DataSet dataSet=  mettingsService.loadMettingsDataSet(params);
	    return ajaxQuery("dataSet", dataSet);
    }
	
    @RequestMapping(value="/createMettings", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public  @ResponseBody Object createMettings(MettingsCreateDto mettingsCreateDto){
    	mettingsCreateDto.setCreatorUserId(SecurityUtil.getUserId().get())
    					 .setCreatorUserName(SecurityUtil.getUserName().get())
    					 .setTenantId(SecurityUtil.getTenantId().get());
		mettingsService.createMettings(mettingsCreateDto);
    	return ajaxDoneSuccess("数据操作成功 ");
    }


    @RequestMapping(value="/updateMettings", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object updateMettings(MettingsUpdateDto mettingsUpdateDto){
		mettingsService.updateMettings(mettingsUpdateDto);
		return ajaxDoneSuccess("数据操作成功 ");
    }
    
    
	//状态_D#0待发布，1报名中，2报名结束，3会议进行中，4会议结束，5取消-1移除 #
    @RequestMapping(value="/startSignup", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object startSignup(Long id){
		mettingsService.setMettingsStatus(id, Byte.valueOf("1"));
		return ajaxDoneSuccess("数据操作成功 ");
    }
    
    @RequestMapping(value="/endSignup", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object endSignup(Long id){
		mettingsService.setMettingsStatus(id, Byte.valueOf("2"));
		return ajaxDoneSuccess("数据操作成功 ");
    }
    @RequestMapping(value="/startMettings", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object startMettings(Long id){
		mettingsService.setMettingsStatus(id, Byte.valueOf("3"));
		return ajaxDoneSuccess("数据操作成功 ");
    }
    @RequestMapping(value="/endMettings", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object endMettings(Long id){
		mettingsService.setMettingsStatus(id, Byte.valueOf("4"));
		return ajaxDoneSuccess("数据操作成功 ");
    }
 
    
    @RequestMapping(value="/cancelMettings", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object cancelMettings(Long id){
		mettingsService.setMettingsStatus(id, Byte.valueOf("5"));
		return ajaxDoneSuccess("数据操作成功 ");
    }
    @RequestMapping(value="/deleteMettings", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object deleteMettings(Long id){
		mettingsService.setMettingsStatus(id, Byte.valueOf("-1"));
		return ajaxDoneSuccess("数据操作成功 ");
    }
    //===============================================================================================
	
    @RequestMapping(value = "/uploadAttaches", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody Object uploadImage(@RequestParam(name = "file", required = false) MultipartFile file,Long metId) {
    	FileResInfo   resInfo =null;
    	try {
    		String fileName=file.getOriginalFilename();
    		if(fileName!=null&&fileName.contains(",")) {
    			ZAlert.newSLE("文件名不能包含符号（,）");
    		}
        	 resInfo = UploadUitl.tranferFile(file, "metattaches");
        	   //更新时添加附件
        	 if(metId!=null) {
        		 mettingsAttachesService.createMettingsAttaches(
 						new MettingsAttachesCreateDto( metId,resInfo.getFileId(), resInfo.getFileOriginName()));
        	 }
        	 
        	 return ajaxQuery("attach", resInfo);
        } catch (Exception e) {
            ZAlert.appRuntimeError(e.getMessage());
        }
        return ajaxQuery("attach", resInfo);
    }
    
    
    @RequestMapping(value="/loadMettingsAttaches", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object loadMettingsAttaches(Long metId){
    	List<MettingsAttaches> attaches = mettingsAttachesService.loadAttachesByMetId(metId, Byte.valueOf("1"));
	    return ajaxQuery("attaches", attaches);
    }
  
    @RequestMapping(value="/deleteMettingsAttaches", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object deleteMettingsAttaches(Long id){
		mettingsAttachesService.setMettingsAttachesStatus(id, Byte.valueOf("-1"));
		return ajaxDoneSuccess("数据操作成功 ");
    }

}